'use strict';
let fs = require('fs');
let Response = require('../../../model/Response');
let RestifyProxy = require('../../../util/RestifyProxy');
let CustomPageBean = require('../../../model/CustomPageBean');
let singleLampService = require('../../../service/operate/singleLampService');
let logger = require('../../../util/LoggerUtil').logger('singleLampController.js');
let URI = {
    SINGLE_UPLOAD:'/asset/bacthInsert',//批量导入
};

/**
 * 单灯管理——页面
 */
module.exports.singleLampPage = function () {
    let response = new Response();
    response.render('back/operate/single_lamp/single_lamp');//html路径
    this.resolve(response);
};

/**
 * 单灯管理——列表数据
 */
module.exports.singleLampList = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await singleLampService.singleLampList(this, params);
        let pageBean = new CustomPageBean(result);
        response.send(pageBean);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};

//单灯管理-Excel导出
module.exports.singleLampExport = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await singleLampService.singleLampExport(this, params);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 生命周期页面批量导入-上传
 */
module.exports.singleLampUpload  = async function () {
    let req = this.req;
    let files = req.files;
    let restify = new RestifyProxy(this);
    let params = req.body;
    for (let key in files) {
        params[key] = {
            value: fs.createReadStream(files[key].path),
            options: {
                filename: files[key].name,
                contentType: files[key].type
            }
        };
    }
    let response = new Response();
    restify.setUrl(URI.SINGLE_UPLOAD)
        .setFormData(params)
        .uploadFile(function (result) {
            response.send(result);
            this.resolve(response);
        }.bind(this));
};
/**
 * 单灯管理-新增页面=========================================================================================================
 */
/*获取项目组路灯地图经纬列表*/
module.exports.LampMapList = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await singleLampService.getLampMapList(this, params);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/*获取项目组路灯地图经纬列表*/
module.exports.IMSIList = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await singleLampService.getIMSIList(this, params);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
module.exports.addSingleLampPage = function () {
    let response = new Response();
    response.render('back/operate/single_lamp/add_single');
    this.resolve(response);
};

module.exports.addSingleLampSave = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await singleLampService.saveSingleLamp(this, params);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};

//生命周期托盘删除
module.exports.deleteSingleLamp = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await singleLampService.deleteSingleLamp(this, params.deviceId);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 单灯管理-详情页面=========================================================================================================
 */
module.exports.singleDetailPage = function () {
    let response = new Response();
    response.render('back/operate/single_lamp/detail_single');
    this.resolve(response);
};
module.exports.refreshNetEnd = async function () {
    let req = this.req;
    let params = req.body;
    let response = new Response();
    try {
        let result = await singleLampService.refreshNetEnd(this, params.deviceId);
        response.send(result);
    } catch (error) {
        logger.error(error);
    }
    this.resolve(response);
};
/**
 * 单灯管理-设置页面=========================================================================================================
 */
module.exports.singleSetPage = function () {
    let response = new Response();
    response.render('back/operate/single_lamp/set_single');
    this.resolve(response);
};
/**
 * 单灯管理-修改页面=========================================================================================================
 */
module.exports.singleModifyPage = function () {
    let response = new Response();
    response.render('back/operate/single_lamp/modify_single');
    this.resolve(response);
};